package com.my.app.tools.ui.awt;

import com.my.app.tools.pojo.em.EditToolType;
import com.my.app.tools.util.AppToolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

public class ScreenShotMainWindow extends JWindow {
    private int orgx, orgy, endx, endy, width, height, screenX, screenY;
    private BufferedImage image, tempImage, saveImage;
    private ScreenShotToolsWindow screenShotTools = null;
    private ScreenShotEditWindow screenShotEditWindow = null;
    private EditToolType editToolType = EditToolType.AGAIN;

    public ScreenShotMainWindow(Rectangle showRect) throws AWTException {
        screenX = showRect.x;
        screenY = showRect.y;
        width = showRect.width;
        height = showRect.height;
        this.setBounds(screenX, screenY, width, height);

        //截取屏幕
        Robot robot = new Robot();
        image = robot.createScreenCapture(new Rectangle(screenX, screenY, width, height));

        // 鼠标十字光标
        this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                //鼠标点击时记录起始点坐标
                orgx = e.getX();
                orgy = e.getY();
                editToolType = EditToolType.AGAIN;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isMetaDown()) {
                    ScreenShotMainWindow.this.cleanImage();
                    ScreenShotMainWindow.this.setVisible(false);
                    return;
                }
                endx = e.getX();
                endy = e.getY();
                Rectangle drawRectangle = AppToolUtil.createRectangle(orgx, orgy, endx, endy);
                if (drawRectangle.width == 0 || drawRectangle.height == 0) {
                    return;
                }
                // 显示工具栏
                showTools(drawRectangle);
                if (saveImage != null) {
                    if (screenShotEditWindow == null) {
                        screenShotEditWindow = new ScreenShotEditWindow(ScreenShotMainWindow.this);
                    }
                    screenShotEditWindow.showImage(saveImage, drawRectangle.x + screenX, drawRectangle.y + screenY);
                    screenShotEditWindow.setVisible(true);
                    screenShotEditWindow.toFront();
                }
            }
        });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                //鼠标拖动时，记录坐标并重绘窗口
                endx = e.getX();
                endy = e.getY();
                Rectangle drawRectangle = AppToolUtil.createRectangle(orgx, orgy, endx, endy);
                if (drawRectangle.width == 0 || drawRectangle.height == 0) {
                    return;
                }
                if (screenShotTools != null || (screenShotTools != null && screenShotEditWindow != null)) {
                    screenShotTools.setVisible(false);
                }
                //临时图像，用于缓冲屏幕区域防止屏幕闪烁
                Image tempImage2 = createImage(width, height);
                // 设置边框参数
                Graphics2D g2d = (Graphics2D) tempImage2.getGraphics();
                g2d.drawImage(tempImage, 0, 0, null);
                g2d.setColor(Color.BLUE);
                g2d.setStroke(new BasicStroke(2));
                g2d.drawRect(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                saveImage = image.getSubimage(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                g2d.drawImage(saveImage, drawRectangle.x, drawRectangle.y, null);
                ScreenShotMainWindow.this.getGraphics().drawImage(tempImage2, 0, 0, ScreenShotMainWindow.this);
            }
        });

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                if (screenShotEditWindow != null) {
                    screenShotEditWindow.dispose();
                }
                if (screenShotTools != null) {
                    screenShotTools.dispose();
                }
            }
        });
    }

    private void showTools(Rectangle drawRectangle) {
        // 鼠标松开时，显示操作窗口
        if (screenShotTools == null) {
            screenShotTools = new ScreenShotToolsWindow(ScreenShotMainWindow.this);
        }
        // 计算工具栏显示位置
        Point mainMiddlePoint = AppToolUtil.calculateMiddlePoint(0, 0, width, height);
        Point editMiddlePoint = AppToolUtil.calculateMiddlePoint(drawRectangle);
        // 默认显示在左上角处
        int toolsX = drawRectangle.x;
        int toolsY = drawRectangle.y - screenShotTools.getHeight();
        // 判断工具栏比编辑框要宽，有可能需要调整显示的x坐标点
        int widthSpace = screenShotTools.getWidth() - drawRectangle.width;
        if (widthSpace > 0) {
            if (editMiddlePoint.x > mainMiddlePoint.x) {
                // 编辑框中心点偏右，工具栏需要右对齐，调整x坐标点
                toolsX -= widthSpace;
            }
        }
        // 编辑框中心点偏上，工具栏需要显示在底部，调整y坐标点
        if (mainMiddlePoint.y > editMiddlePoint.y) {
            toolsY += screenShotTools.getHeight() + drawRectangle.height;
            toolsY += 5;
        } else {
            toolsY -= 5;
        }
        screenShotTools.setLocation(toolsX + screenX, toolsY + screenY);
        screenShotTools.setVisible(true);
        screenShotTools.toFront();
    }

    @Override
    public void paint(Graphics g) {
        RescaleOp ro = new RescaleOp(0.6f, 0, null);
        tempImage = ro.filter(image, null);
        g.drawImage(tempImage, 0, 0, this);
    }

    public BufferedImage getImage() {
        if (screenShotEditWindow != null) {
            return screenShotEditWindow.getImage();
        }
        return this.saveImage;
    }

    public void cleanImage() {
        if (screenShotEditWindow != null) {
            screenShotEditWindow.setImage(null);
        }
        this.saveImage = null;
    }

    public EditToolType getEditToolType() {
        return editToolType;
    }

    public void setEditToolType(EditToolType editToolType) {
        this.editToolType = editToolType;
    }

}
